%% "Econometrics of the Hodrick-Prescott Filter," Robert de Jong and Neslihan Sakarya (2015)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% This program compiles Figure 2 (in Appendix 1) of the paper

%%
clear all;
close all;
clc;
T=50; %% total number of observations
lm=1600; %% the smoothing parameter, lambda

%% Defining the function g_T_lambda, it is denoted as g

r=zeros(T,T);
dg=zeros(T,1);

for m=1:T;
    for j=1:T;
        
        r(m,j)=sqrt(2)*cos(pi*(j-1)*(m-(1/2))/T); % the matrix for sqrt(2)*cos(pi*(j-1)*(m-(1/2))/T), used in g function 
        dg(j)=sin(pi*(j-1)/(2*T))^2*cos(pi*(j-1)/(2*T))*(1+16*lm*sin(pi*(j-1)/(2*T))^4)^(-1); %q1(j)/(1+16*lm*sin(pi*(j-1)/(2*T))^4), used in g function
    end
end

g=T^(-1)*r*dg; %% r*dg is the sum j=1 to T (sqrt(2)*cos(pi*(j-1)*(m-(1/2))/T)*q1(j)*(1+16*lm*sin(pi*(j-1)/(2*T))^4)^(-1))


%% The Results that are displayed on the Command Window

printmat(g, 'g_T_lambda'); %the values of g function with the name g_T_lambda



t=1:1:T;
plot(t,g)
title('The Function g')
xlabel('m')
ylabel('Function Values')
